	// Read a page's GET URL variables and return them as an associative array.
	function getVars(url)
	{
	    var formData = new FormData();
	    var split;
	    $.each(url.split("&"), function(key, value) {
	        split = value.split("=");
	        formData.append(split[0], decodeURIComponent(split[1].replace(/\+/g, " ")));
	    });
	
	    return formData;
	}
	
	// Variable to store your files
	var files;
	
	$( document ).delegate('#input-image','change', prepareUpload);
	
	// Grab the files and set them to our variable
	function prepareUpload(event)
	{
	    files = event.target.files;
	}

	/* 
	 * JTable empleados
	 */
	$(document).ready(function() {              
	    //setup the jtable that will display the results
		
		
		
	    $('#TablaDispositivos').jtable({
	        title: ' ',
	        selecting: true, //Enable selecting 
	        paging: true, //Enable paging
	        pageSize: 10, //Set page size (default: 10)
	        sorting: true, //Enable sorting
            defaultSorting: 'Nombre ASC', //Set default sorting
	        actions: {
	        	listAction: 'getDispositivos',
                createAction: function (postData) {
                    var formData = getVars(postData);

                    if($('#input-image').val() !== ""){
                        formData.append("userfile", $('#input-image').get(0).files[0]);
                    }

                    return $.Deferred(function ($dfd) {
                        $.ajax({ 
                            url: 'addDispositivo',
                            type: 'POST',
                            dataType: 'json',
                            data: formData,
                            processData: false, // Don't process the files
                            contentType: false, // Set content type to false as jQuery will tell the server its a query string request
                            success: function (data) {
                                $dfd.resolve(data);
                                $('#table-container').jtable('load');
                            },
                            error: function () {
                                $dfd.reject();
                            }
                        });
                    });
                },
	            updateAction: function (postData) {
                    var formData = getVars(postData);

                    if($('#input-image').val() !== ""){
                        formData.append("userfile", $('#input-image').get(0).files[0]);
                    }

                    return $.Deferred(function ($dfd) {
                        $.ajax({ 
                            url: 'updateDispositivo',
                            type: 'POST',
                            dataType: 'json',
                            data: formData,
                            processData: false, // Don't process the files
                            contentType: false, // Set content type to false as jQuery will tell the server its a query string request
                            success: function (data) {
                                $dfd.resolve(data);
                                
                                $('#table-container').jtable('load');
                                
                            },
                            error: function () {
                                $dfd.reject();
                            }
                        });
                    });
                },
	        },
	        fields: {
	        	Dispid: {
	                key: true,
	                visibility: 'hidden',
	            },
	        	Dispositivoid: {
	                title: 'Nº de Serie',
	                width: '15%',
	                inputClass : 'validate[required]',
	    	        sorting: true,
	            },
	            Nombre: {
	                title: 'Nombre',
	                width: '20%',
	                sorting: true
	            },
	            Estado: {
	            	title: 'Estado',
		            width: '10%',
		            options:  { '0': 'Disponible', '1': 'En uso', '2': 'Averiado', '3': 'No disponible'},
		            defaultValue:'0',
		            sorting: true
	            },
	            Solid: {
	                title: 'Sol. Id',
	                width: '9%',
	                sorting: false,
	                edit:false,
	            	create:false,
	            },
	            Fechamod: {
	            	 title: 'F. Modificación',
		             width: '15%',
		             create: false,
		             sorting: false,
		             edit:false
	            },
	            Observaciones: {
	            	title: 'Observaciones',
	            	sorting: false,
	            	edit: true,
	            	type: 'textarea',
	            	inputClass: 'textarea_disp'
	            },
	            ImagenMin: {
	            	width: '10%',
                    title: 'Image',
                    type: 'file',
                    create: false,
                    edit: false,
                    sorting: false,
                    list: true,
                    display: function(data){
                        return '<img src="'+ data.record.Imagen +'?'+Date.now()+'" width="75px" height="75px" class="preview">';
                    },
                   
                    listClass: "class-row-center"
                },
	            Imagen: {
                    title: 'Imagen del producto',
                    edit:true,
                    list:false,
                    create: true,
                    input: function(data) {
                        html = '<input type ="file" id="input-image" name="imagen" accept="image/*"  />';
                        return html;
                    }
                }
	        },
	        
	      //Initialize validation logic when a form is created
            formCreated: function (event, data) {
                data.form.validationEngine();
            },
            //Validate form when it is being submitted
            formSubmitting: function (event, data) {
                return data.form.validationEngine('validate');
            },
            //Dispose validation logic when form is closed
            formClosed: function (event, data) {
                data.form.validationEngine('hide');
                data.form.validationEngine('detach');
            },
	        //Register to selectionChanged event to hanlde events                                     
	        recordAdded: function(event, data){
	            //after record insertion, reload the records
	            $('#TablaDispositivos').jtable('load');
	        },
	        recordUpdated: function(event, data){
	            //after record updation, reload the records
	            $('#TablaDispositivos').jtable('load');
	        },
	        rowInserted: function (event, data) { 
                //After child row loads. Check if the review belongs to the member logged in. If not remove the edit/delete buttons
                if (data.record.Solid != null) { 
                    data.row.find('.jtable-edit-command-button').hide(); 
                }
	        }
	    });
	    $('#TablaDispositivos').jtable('load');              
	                
	});

